/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package flowvis;

import flowsimulation.FlowSimulator;
import gui.HedgehogRendererPanel;
import javax.media.opengl.GL;

/**
 *
 * @author michel
 *
 * Renderer that draws velocity vectors as short line segments.
 *
 */
public class HedgehogRenderer extends Renderer {
    
    private static boolean VISIBLE = false;
    
    //Parameters of the Panel
    float vec_scale = 1000; // scaling factor = default value
    int coloring = 0;       // coloring mode: 0=white, 1=colored = checked and unchecked
    HedgehogRendererPanel panel;    // GUI panel for this renderer

    public HedgehogRenderer(FlowSimulator sim) {
        super(sim);
        panel = new HedgehogRendererPanel(this);
    }

    public HedgehogRendererPanel getPanel() {
        return panel;
    }

    public void setScale(float s) {
        vec_scale = s;
    }

    public float getScale() {
        return vec_scale;
    }

    /**
     * 
     * @param visible 
     */
    public static void setVisible( boolean visible ){ VISIBLE = visible; }
    /**
     * 
     * @return 
     */
    public static boolean getVisible(){ return VISIBLE; }

    public void setColoring(boolean flag) {
        if (flag) {
            coloring = 1;
        } else {
            coloring = 0;
        }
    }

    public boolean getColoring() {
        return coloring > 0 ? true : false;
    }

    //direction_to_color: Set the current color by mapping a direction vector (x,y), using
    //                    the color mapping method 'method'. If method==1, map the vector direction
    //                    using a rainbow colormap. If method==0, simply use the white color
    // (x,y) is the direction vector using the velocity x and y at the point (x,y)
    void direction_to_color(GL gl, float x, float y, int method) {
        float r, g, b;
        if (method == 1) {
            float f = (float) (Math.atan2(y, x) / Math.PI + 1);
            r = f;
            if (r > 1) {
                r = 2 - r;
            }
            g = f + .66667f;
            if (g > 2) {
                g -= 2;
            }
            if (g > 1) {
                g = 2 - g;
            }
            b = f + 2 * .66667f;
            if (b > 2) {
                b -= 2;
            }
            if (b > 1) {
                b = 2 - b;
            }
        // set the value of red, green and blue to 1, so that the vector are whites
        } else { // method==0
            r = g = b = 1;
        }
        gl.glColor3f(r, g, b);
    }

    @Override
    public void visualize(GL gl) {
        
        //if the checkbox visible is not checked
        if (!getVisible()) {
            // don't do anything when this renderer is not visible
            return;
        }
        
        int i, j, idx;
        //DIM = number of vector on each column and on each row
        int DIM = simulator.getGridSize();
        double/*fftw_real*/ wn = getWinWidth() / (double) (DIM + 1);   // Grid cell width
        double/*fftw_real*/ hn = getWinHeight() / (double) (DIM + 1);  // Grid cell heigh
        
        double[] vx = simulator.getVelocityX();
        double[] vy = simulator.getVelocityY();

        gl.glBegin(GL.GL_LINES);
        //for all lines and columns
        for (i = 0; i < DIM; i++) {
            for (j = 0; j < DIM; j++) {
                idx = (j * DIM) + i;
                //explained before
                direction_to_color(gl, (float) (double) vx[idx], (float) (double) vy[idx], coloring);
                //Display the vectors on the grid
                gl.glVertex2d(wn + i * wn, hn + j * hn);
                //Change the position of the vector (velocity) depending on the vector_scale value 
                //(default 1000)and also depending on the mouse event.
                gl.glVertex2d((wn + i * wn) + vec_scale * vx[idx], (hn + j * hn) + vec_scale * vy[idx]);
            }
        }
        gl.glEnd();

    }
}
